      SUBROUTINE RWPROF(FREQ,NPW,NPB,
     1                 DEPW,SVPW,DBPWLW,cgwnsw,cwnsw,RHOW,GRHOW,
     2                 DEPB,SVPB,DBPWLB,cgwnsb,cwnsb,RHOB,GRHOB,
     3                 LUSVP,MX)
C
C     THIS SUBROUTINE READS A DEPTH, SOUND SPEED, ATTENUATION AND 
C     DENSITY PROFILE IN THE WATER AND SEDIMENT. IT COMPUTES THE 
C     WAVE NUMBER SQUARED TABLES BY CALLING CWNS AND WRITES THE
C     RESULTS ON THE PRINT AND LOG FILES AND ON UNIT=LUSVP FOR LATER 
C     USE IN THE FIELD CALCULATION. 
C     
      IMPLICIT REAL*8 (A-H,O-Z)
      DIMENSION DEPW(MX),SVPW(MX),DBPWLW(MX),RHOW(MX),GRHOW(MX)
      DIMENSION DEPB(MX),SVPB(MX),DBPWLB(MX),RHOB(MX),GRHOB(MX)
C
      COMPLEX*16 cgwnsw(MX),cwnsw(MX)
      COMPLEX*16 cgwnsb(MX),cwnsb(MX)
C
      COMMON /BLKEVN/ HB,CW,CB,FKW,FKB,ROHW,ROHB,ATEN
C
      PI=2.*ACOS(0.0)
C
       WRITE(9,330) 
  330  FORMAT(1X,' THE SOUND VELOCITY-DENSITY PROFILE IN THE WATER',
     1        ' IS AS ','FOLLOWS:',//2X,'DEPTH (M) ',3X,
     2        'SOUND SPEED (M/SEC)',4X,
     3        'ATTENUATION (DB/WL)',4X,
     4        'DENSITY (GM/CM**3)',/)
C
C     WATER PROFILE IS READ HERE
C
      DO 340 K=1,NPW
      READ(5,*) DEPW(K),SVPW(K),DBPWLW(K),RHOW(K)
      WRITE(9,335) DEPW(K),SVPW(K),DBPWLW(K),RHOW(K)
  335 FORMAT(1X,F10.2,7X,F10.4,13X,E10.2,10X,F10.4)
  340 CONTINUE
C
      WRITE(9,342)
  342 FORMAT(/)
      WRITE(9,345) 
  345 FORMAT(1X,' THE SOUND VELOCITY-DENSITY PROFILE IN THE BOTTOM',
     1        ' IS AS ','FOLLOWS:',//2X,'DEPTH (M) ',3X,
     2        'SOUND SPEED (M/SEC)',4X,
     3        'ATTENUATION (DB/WL)',4X,
     4        'DENSITY (GM/CM**3)',/)
C
C     SEDIMENT PROFILE IS READ HERE
C
      DO 360 K=1,NPB
      READ(5,*) DEPB(K),SVPB(K),DBPWLB(K),RHOB(K)
      WRITE(9,355) DEPB(K),SVPB(K),DBPWLB(K),RHOB(K)
  355 FORMAT(1X,F10.2,7X,F10.4,13X,F10.4,10X,F10.4)
  360 CONTINUE
C
C      GENERATE COEFFICIENTS IN PIECEWISE LINEAR REPRESENTATION
C      OF THE SQUARE OF THE WAVE NUMBER FROM THE WATER AND
C      BOTTOM PROFILES
C
      CALL CWNS(FREQ,DEPW,SVPW,DBPWLW,cgwnsw,cwnsw,NPW,MX)
      CALL CWNS(FREQ,DEPB,SVPB,DBPWLB,cgwnsb,cwnsb,NPB,MX)
C
C     FIND THE AVERAGE OF THE SQUARE OF THE WAVE NUMBER
C     FROM THE WATER PROFILE
C
      HNPW=DEPW(NPW)
      CALL BOUNDS(DEPW,cgwnsw,cwnsw,NPW,MX,HNPW,fminw,AVGW,fmaxw,LHW)
C
C     COMPUTE THE AVERAGE WAVE NUMBER AND CORRESPONDING SOUND SPEED  
C
      FKWAVG=SQRT(AVGW)
      CWAVG=2.0*PI*FREQ/FKWAVG
C
      WRITE(9,342)
      WRITE(6,370) AVGW,CWAVG
      WRITE(9,370) AVGW,CWAVG
C
  370 FORMAT(1X,/' AVERAGE (WAVE NUMBER)**2 = ',E12.5,
     1       '  OR ',4PE12.5 ,' M/SEC')
C
      WRITE(6,375) FKWAVG
      WRITE(9,375) FKWAVG
  375 FORMAT(1X,' AVERAGE WAVE NUMBER FROM THE WATER PROFILE= ',1P,
     1        E13.5,' 1/M')
C
C     FIND THE AVERAGE OF THE SQUARE OF THE WAVE NUMBER
C     FROM THE BOTTOM PROFILE
C
      HNPB=DEPB(NPB)
      CALL BOUNDS(DEPB,cgwnsb,cwnsb,NPB,MX,HNPB,fminb,AVGB,fmaxb,LHB)
      FKBAVG=SQRT(AVGB)
      CBAVG=2.0*PI*FREQ/FKBAVG
C
      WRITE(9,342)
      WRITE(6,370) AVGB,CBAVG
      WRITE(9,370) AVGB,CBAVG
C
      WRITE(6,376) FKBAVG
      WRITE(9,376) FKBAVG
  376 FORMAT(1X,' AVERAGE WAVE NUMBER FROM THE BOTTOM PROFILE= ',1P,
     1        E13.5,' 1/M')
C
      WRITE(9,342)
      WRITE(9,380)
      WRITE(6,380)
  380 FORMAT(1X,'THE PIECEWISE LINEAR (WAVE NUMBER)**2 ',
     1       'IN FROM THE WATER PROFILE IS AS FOLLOWS:',//2X,
     2       'DEPTH (M)',6X,'(WAVE NUMBER)**2',10X,'GRADIENT'/)
C
C     PRINT WAVE NUMBER SQUARED TABLE FROM THE WATER PROFILE,
C     DEFINE LOGARITHMIC GRADIENTS OF THE WATER DENSITY PROFILE
C     AND WRITE RESULTS ON LUSVP FOR USE IN THE MAIN PROGRAM COUPLE
C
      WRITE(LUSVP,*) NPW,NPB
C
      NPWM1=NPW-1
      DO 400 K=1,NPWM1
      GRHOW(K)=DLOG(RHOW(K+1)/RHOW(K))/(DEPW(K+1)-DEPW(K))
C
      WRITE(6,390) DEPW(K),cwnsw(K)+cgwnsw(K)*DEPW(K),cgwnsw(K)
      WRITE(9,390) DEPW(K),cwnsw(K)+cgwnsw(K)*DEPW(K),cgwnsw(K)
C
      WRITE(LUSVP,*) DEPW(K),cwnsw(K),cgwnsw(K),RHOW(K),GRHOW(K)
  390 FORMAT(1X,F10.2,2X,F10.4,E12.4,2X,2E12.4)
  400 CONTINUE
C
C     FOLLOWING LINE WAS ADDED TO BE LIKE BOTTOM PROFILE
C     IT WAS PREVIOUSLY UNDEFINED (R.B.EVANS,SAIC,4/15/98)
C
      GRHOW(NPW)=GRHOW(NPWM1)
C
      WRITE(6,390) DEPW(NPW),cwnsw(NPWM1)+cgwnsw(NPWM1)*DEPW(NPW),
     1             cgwnsw(NPW)
      WRITE(9,390) DEPW(NPW),cwnsw(NPWM1)+cgwnsw(NPWM1)*DEPW(NPW),
     1             cgwnsw(NPW)
C
      WRITE(LUSVP,*) DEPW(NPW),cwnsw(NPW),cgwnsw(NPW),RHOW(NPW),
     1               GRHOW(NPW)
C
      WRITE(9,342)
      WRITE(6,342)
      WRITE(9,405)
      WRITE(6,405)
  405 FORMAT(1X,'THE PIECEWISE LINEAR (WAVE NUMBER)**2 ',
     1       'IN THE BOTTOM IS AS FOLLOWS:',//2X,'DEPTH (M)',6X,
     2       '(WAVE NUMBER)**2',10X,'GRADIENT'/)
C
      NPBM1=NPB-1
C
C     PRINT WAVE NUMBER SQUARED TABLE FROM THE BOTTOM PROFILE,
C     DEFINE LOGARITHMIC GRADIENTS OF THE BOTTOM DENSITY PROFILE
C     AND WRITE RESULTS ON LUSVP FOR USE IN THE MAIN PROGRAM COUPLE
C
      DO 420 K=1,NPBM1
      GRHOB(K)=DLOG(RHOB(K+1)/RHOB(K))/(DEPB(K+1)-DEPB(K))
      WRITE(6,390) DEPB(K),cwnsb(K)+cgwnsb(K)*DEPB(K),cgwnsb(K)
      WRITE(9,390) DEPB(K),cwnsb(K)+cgwnsb(K)*DEPB(K),cgwnsb(K)
      WRITE(LUSVP,*) DEPB(K),cwnsb(K),cgwnsb(K),RHOB(K),GRHOB(K)
  420 CONTINUE
      GRHOB(NPB)=GRHOB(NPBM1)
      WRITE(6,390) DEPB(NPB),cwnsb(NPBM1)+cgwnsb(NPBM1)*DEPB(NPB)
      WRITE(9,390) DEPB(NPB),cwnsb(NPBM1)+cgwnsb(NPBM1)*DEPB(NPB)
      WRITE(LUSVP,*) DEPB(NPB),cwnsb(NPB),cgwnsb(NPB),RHOB(NPB),
     1               GRHOB(NPB)
C
C
      RETURN
      END
